<?php

namespace app\controller;

use app\BaseController;

class Echarts extends BaseController
{
    // 定义城市常量集合
    private const CITIES = [
        'bj' => '北京',
        'sh' => '上海',
        'gz' => '广州',
        'sz' => '深圳',
        'cq' => '重庆',
        'hz' => '杭州',
    ];

    public function index()
    {
        return json([
            "code" => 200,
            "msg" => "success",
            "data" => [
                "bigAreaData" => $this->generateChartData(),
                "servicesData" => $this->generateChartData(),
                "handleWarningData" => $this->generateChartData(),
                "totalCountData" => $this->generateTotalCountData(),
            ],
        ]);
    }

    private function generateTotalCountData(): array
    {
        $data = [];
        $total = 0;

        foreach (self::CITIES as $key => $name) {
            $count = rand(4000000, 9999999);
            // 添加数字格式化：每3位加逗号
            $data[$key] = number_format($count);
            $total += $count;
        }

        // 格式化总计数字
        $data['total'] = number_format($total);
        return $data;
    }

    private function generateChartData(): array
    {
        $data = [];
        $index = 0;

        foreach (self::CITIES as $key => $name) {
            $data[] = [
                'id' => $index++,
                'name' => $name,
                'value' => rand(20, 100),
            ];
        }

        return $data;
    }
}
